﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Zjs_VoteSys.Models;
using Zjs_VoteSys.Models.Database;
using Zjs_VoteSys.Service;

namespace Zjs_VoteSys.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class HomeController : ControllerBase
    {
        //包源 名称：nuget.org 源：https://api.nuget.org/v3/index.json
        //数据库连接 Scaffold-DbContext "Data Source=.;Initial Catalog=Zjs_VoteSys;Integrated Security=SSPI" Microsoft.EntityFrameworkCore.SqlServer -o ./Models/Database -Force
        //定义数据库上下文
        private readonly Zjs_VoteSysContext _db;
        //注入jwt服务
        private readonly IJwtService _jwtService;
        //注入用户服务服务
        private readonly IUserService _userService;
        //注入Redis服务
        //private readonly IRedisService _redisService;

       
        /// <summary>
        /// 构造方法 构造注入
        /// </summary>
        public HomeController(Zjs_VoteSysContext db, IJwtService jwtService, IUserService userService)
        {
            _db = db;
            _jwtService = jwtService;
            _userService = userService;

        }
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public string Reg(Risrequest request)
        {
            if (_userService.TfUserEmail(request.Email))
                return "用户存在";
            if (request.Password != request.CirPassword)
                return "两次密码输入不一致";
            //使用服务
           
            var email= _userService.Reg(request.Email,request.Password);
            if (email !=null)
            {
                return "注册成功";
            }
            return "注册失败";
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="email"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        [HttpPost]
        public string Login(string email,string password)
        {

            var user = _userService.GetphoneByEmail(email);
            if (user == null)
                return "用户不存在";

            //生成md5字符串                      输入的         数据库的盐值
            var md5 = MD5Helper.MD5Encrypt32(password + user.Salt.ToString());
            if (user.Password != md5)
                return "密码不正确";

            return _jwtService.CreateToken(user.Id);

        }
        /// <summary>
        /// 添加公司信息
        /// </summary>
        /// <param name="CompanyLogo"></param>
        /// <param name="Name"></param>
        /// <param name="Description"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public string AddCom(string CompanyLogo, string Name, string Description)
        {
            var Id = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            return _userService.AddCom(Convert.ToInt32(Id),CompanyLogo, Name, Description);

        }
        /// <summary>
        /// 上传公司获奖情况 一个奖项只能上传一个
        /// </summary>
        /// <param name="companyId"></param>
        /// <param name="awardCategoryId">奖项类别 1工程 2产品 3研发</param>
        /// <param name="prizeCategoryId">获奖类别 1优秀奖 2最佳奖</param>
        /// <param name="projectImage"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public string AddAward(int companyId, int awardCategoryId, int prizeCategoryId, string projectImage)
        {
            var Id = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            return _userService.AddAward(Convert.ToInt32(Id),companyId, awardCategoryId, prizeCategoryId, projectImage);
        }
        /// <summary>
        /// 校验用户是否上传过该类别的投票项目
        /// </summary>
        /// <param name="awardCategoryId">奖项类别 1工程 2产品 3研发</param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public string TfAward(int awardCategoryId)
        {
            var Id = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            return _userService.TfAward(Convert.ToInt32(Id), awardCategoryId);
        }
        /// <summary>
        /// 审核接口
        /// </summary>
        /// <param name="examId"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        [HttpPost]
        public string Examine(int examId, string state)
        {
            return _userService.Examine(examId, state);
        }
        /// <summary>
        /// 获取企业详情（投票项目详情）信息接口 
        /// </summary>
        /// <param name="projectid">项目id</param>
        /// <returns></returns>
        [HttpPost]
        public List<ModelsProject> ModelsProject(int projectid)
        {
            return _userService.ModelsProject(projectid);
        }
        /// <summary>
        /// 获取投票项目列表接口 
        /// </summary>
        /// <param name="projectid"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public string Votes(int projectid)
        {
            var Id = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            return _userService.Votes(Convert.ToInt32(Id), projectid);
        }
        public GetProResponse getProResponse(int AwardCategoryId, string Keyword, int PageIndex, int PageSize)
        {
            return _userService.getProResponse(AwardCategoryId, Keyword, PageIndex, PageSize);
        }
        public GetProResponse getProjectResponse(int companyid, int PageIndex, int PageSize)
        {
            return _userService.getProjectResponse(companyid, PageIndex, PageSize);
        }
    }
}
